package com.life.ai.server.modules.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.life.ai.server.common.domain.PageQuery;
import com.life.ai.server.common.domain.R;
import com.life.ai.server.framework.syslog.annotation.SysLog;
import com.life.ai.server.modules.system.entity.SysMenu;
import com.life.ai.server.modules.system.model.vo.SysMenuNodeVO;
import com.life.ai.server.modules.system.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 菜单权限 前端控制器
 *
 * @author kyle
 * @since  2024-06-14
 */
@Api(tags = "菜单权限-接口列表")
@RestController
@RequestMapping("/system/menu")
public class SysMenuController {

    @Resource
    private SysMenuService sysMenuService;

    /**
     * 菜单权限-分页查询
     */
    @SaCheckPermission("system:menu:list")
    @ApiOperation(value = "分页查询")
    @GetMapping("/page")
    public R<Page<SysMenu>> queryPage(PageQuery pageQuery, SysMenu sysMenu) {
        Page<SysMenu> page = sysMenuService.selectMenuPage(pageQuery, sysMenu);
        return R.ok(page);
    }

    /**
     * 获取菜单列表
     */
    @SaCheckPermission("system:menu:list")
    @ApiOperation(value = "菜单列表")
    @GetMapping("/list")
    public R<List<SysMenu>> list(SysMenu menu) {
        List<SysMenu> menus = sysMenuService.selectMenuList(menu);
        return R.ok(menus);
    }


    @SaCheckPermission("system:menu:list")
    @ApiOperation(value = "菜单列表(树形)")
    @GetMapping("/tree")
    public R<List<SysMenuNodeVO>> tree(SysMenu menu) {
        List<SysMenuNodeVO> menuNodes = sysMenuService.selectMenuTree(menu);
        return R.ok(menuNodes);
    }


    /**
     * 菜单权限-单条查询
     */
    @SaCheckPermission("system:menu:query")
    @ApiOperation("菜单详情")
    @GetMapping("/{id}")
    public R<SysMenu> queryOne(@PathVariable("id") Long id) {
        return R.ok(sysMenuService.getById(id));
    }

    /**
     * 新增菜单
     */
    @SaCheckPermission("system:menu:add")
    @SysLog("新增菜单")
    @ApiOperation(value = "新增菜单")
    @PostMapping("/add")
    public R<Void> add(@Validated @RequestBody SysMenu sysMenu) {
        sysMenuService.addMenu(sysMenu);
        return R.ok();
    }

    /**
     * 修改菜单
     */
    @SaCheckPermission("system:menu:update")
    @SysLog("修改菜单")
    @ApiOperation(value = "修改菜单")
    @PutMapping("/update")
    public R<Void> update(@Validated @RequestBody SysMenu sysMenu) {
        sysMenuService.updateMenu(sysMenu);
        return R.ok();
    }

    /**
     * 删除菜单
     *
     * @param id 菜单ID
     */
    @SaCheckPermission("system:menu:delete")
    @SysLog("删除菜单")
    @ApiOperation(value = "删除菜单")
    @DeleteMapping("/delete/{id}")
    public R<Void> remove(@PathVariable("id") Long id) {
        sysMenuService.deleteMenuById(id);
        return R.ok();
    }

}